Skip to content

Commit

Permalink
Merge pull request #59 from codelerity/custom-types
Browse files Browse the repository at this point in the history
Delegate to parent container to calculate types
  • Loading branch information
neilcsmith-net authored Jun 26, 2024
2 parents 29cc682 + 235e748 commit fcaf359
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public interface Component {
* component info, and property values, in that order. It may also add
* custom annotations.
* <p>
* The component should delegate to
* {@link Container#getType(org.praxislive.core.Component)} to find its type
* rather than relying directly on {@link ComponentInfo#KEY_COMPONENT_TYPE}.
* <p>
* The default implementation of this method does nothing.
*
* @param writer TreeWriter to write to
Expand Down
20 changes: 20 additions & 0 deletions praxiscore-api/src/main/java/org/praxislive/core/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package org.praxislive.core;

import java.util.Optional;
import java.util.stream.Stream;
import org.praxislive.core.protocols.ContainerProtocol;

Expand Down Expand Up @@ -79,4 +80,23 @@ public default void write(TreeWriter writer) {
// no op
}

/**
* Get the {@link ComponentType} of the provided child. The default
* implementation looks for {@link ComponentInfo#KEY_COMPONENT_TYPE} in the
* child's info. Container's may override to provide a more efficient or
* suitable result.
* <p>
* The default implementation does not check if the provided component is
* actually a child of this container.
*
* @param child child component
* @return component type, or null if unavailable
*/
public default ComponentType getType(Component child) {
return Optional.ofNullable(child.getInfo())
.map(info -> info.properties().get(ComponentInfo.KEY_COMPONENT_TYPE))
.flatMap(ComponentType::from)
.orElse(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private static ControlInfo propertyFromData(PMap data) {
var inputs = PArray.from(data.asMap().getOrDefault(KEY_INPUTS, PArray.EMPTY))
.map(a -> a.asListOf(ArgumentInfo.class))
.orElseThrow(IllegalArgumentException::new);
var defaults = PArray.from(data.asMap().getOrDefault(KEY_INPUTS, PArray.EMPTY))
var defaults = PArray.from(data.asMap().getOrDefault(KEY_DEFAULTS, PArray.EMPTY))
.map(a -> a.asList())
.orElseThrow(IllegalArgumentException::new);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,23 @@ public void write(TreeWriter writer) {
}

protected final void writeTypeAndInfo(TreeWriter writer) {
ComponentInfo info = getInfo();
if (info == null) {
return;
ComponentType type;
if (parent == null) {
// assume we're a root?!
type = Optional.ofNullable(getInfo())
.map(info -> info.properties().get(ComponentInfo.KEY_COMPONENT_TYPE))
.flatMap(ComponentType::from)
.orElse(null);
} else {
type = parent.getType(this);
}
ComponentType type = Optional.ofNullable(
info.properties().get(ComponentInfo.KEY_COMPONENT_TYPE))
.flatMap(ComponentType::from)
.orElse(null);
if (type != null) {
writer.writeType(type);
}
writer.writeInfo(info);
ComponentInfo info = getInfo();
if (info != null) {
writer.writeInfo(info);
}
}

protected final void writeMeta(TreeWriter writer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package org.praxislive.base;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -31,6 +32,7 @@
import org.praxislive.core.Call;
import org.praxislive.core.Component;
import org.praxislive.core.ComponentAddress;
import org.praxislive.core.ComponentType;
import org.praxislive.core.Connection;
import org.praxislive.core.Container;
import org.praxislive.core.Control;
Expand Down Expand Up @@ -63,10 +65,12 @@ public abstract class AbstractContainer extends AbstractComponent implements Con
private final static System.Logger LOG = System.getLogger(AbstractContainer.class.getName());

private final Map<String, Component> childMap;
private final Map<Component, ComponentType> childTypeMap;
private final Set<Connection> connections;

protected AbstractContainer() {
childMap = new LinkedHashMap<>();
childTypeMap = new HashMap<>();
connections = new LinkedHashSet<>();
registerControl(ContainerProtocol.ADD_CHILD, new AddChildControl());
registerControl(ContainerProtocol.REMOVE_CHILD, new RemoveChildControl());
Expand Down Expand Up @@ -102,6 +106,11 @@ public ComponentAddress getAddress(Component child) {
}
}

@Override
public ComponentType getType(Component child) {
return childTypeMap.computeIfAbsent(child, Container.super::getType);
}

@Override
public void hierarchyChanged() {
childMap.values().forEach(Component::hierarchyChanged);
Expand Down Expand Up @@ -156,6 +165,7 @@ protected Component removeChild(String id) {
LOG.log(System.Logger.Level.ERROR, "Child throwing Veto on removal", ex);
}
child.hierarchyChanged();
childTypeMap.remove(child);
}
return child;
}
Expand Down Expand Up @@ -231,11 +241,15 @@ protected Call processResponse(Call call) throws Exception {
if (args.size() < 1) {
throw new IllegalArgumentException("Invalid response");
}
Component c = PReference.from(args.get(0))
Component child = PReference.from(args.get(0))
.flatMap(r -> r.as(Component.class))
.orElseThrow();
Call active = getActiveCall();
addChild(active.args().get(0).toString(), c);
addChild(active.args().get(0).toString(), child);
ComponentType type = ComponentType.from(active.args().get(1)).orElse(null);
if (type != null) {
childTypeMap.put(child, type);
}
return active.reply();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package org.praxislive.code;

import java.util.Optional;
import org.praxislive.base.MetaProperty;
import org.praxislive.core.Component;
import org.praxislive.core.ComponentAddress;
Expand All @@ -33,6 +34,7 @@
import org.praxislive.core.Port;
import org.praxislive.core.VetoException;
import org.praxislive.core.ComponentInfo;
import org.praxislive.core.ComponentType;
import org.praxislive.core.ControlInfo;
import org.praxislive.core.ThreadContext;
import org.praxislive.core.TreeWriter;
Expand Down Expand Up @@ -125,7 +127,19 @@ public ComponentInfo getInfo() {

@Override
public void write(TreeWriter writer) {
writer.writeType(codeCtxt.getComponentType());
ComponentType type;
if (parent == null) {
// assume we're a root?!
type = Optional.ofNullable(getInfo())
.map(info -> info.properties().get(ComponentInfo.KEY_COMPONENT_TYPE))
.flatMap(ComponentType::from)
.orElse(null);
} else {
type = parent.getType(this);
}
if (type != null) {
writer.writeType(type);
}
writer.writeInfo(getInfo());
codeCtxt.writeDescriptors(writer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2023 Neil C Smith.
* Copyright 2024 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 only, as
Expand All @@ -19,16 +19,43 @@
* Please visit https://www.praxislive.org if you need additional information or
* have any questions.
*/

package org.praxislive.code;

import java.util.stream.Stream;
import org.praxislive.core.ControlInfo;
import org.praxislive.core.services.ComponentFactory;
import org.praxislive.core.services.ComponentFactoryService;
import org.praxislive.core.services.Service;

/**
*
*
* A factory service for code components. Provides the same API as
* {@link ComponentFactoryService} for use as a redirect with
* {@link ComponentFactory#componentRedirect()}.
*/
public class CodeComponentFactoryService extends ComponentFactoryService {


public final class CodeComponentFactoryService implements Service {

/**
* Control ID of the new instance control.
*/
public final static String NEW_INSTANCE = ComponentFactoryService.NEW_INSTANCE;

/**
* ControlInfo for the new instance control.
*/
public final static ControlInfo NEW_INSTANCE_INFO
= ComponentFactoryService.NEW_INSTANCE_INFO;

@Override
public Stream<String> controls() {
return Stream.of(NEW_INSTANCE);
}

@Override
public ControlInfo getControlInfo(String control) {
if (NEW_INSTANCE.equals(control)) {
return NEW_INSTANCE_INFO;
}
throw new IllegalArgumentException();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2023 Neil C Smith.
* Copyright 2024 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 only, as
Expand Down Expand Up @@ -29,7 +29,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;
Expand All @@ -44,7 +43,6 @@
import org.praxislive.core.PacketRouter;
import org.praxislive.core.Port;
import org.praxislive.core.ComponentInfo;
import org.praxislive.core.ComponentType;
import org.praxislive.core.TreeWriter;
import org.praxislive.core.Value;
import org.praxislive.core.services.Service;
Expand All @@ -70,7 +68,6 @@ public abstract class CodeContext<D extends CodeDelegate> {
private final Map<String, ReferenceDescriptor<?>> refs;
private final List<Descriptor> descriptors;
private final ComponentInfo info;
private final ComponentType componentType;

private final D delegate;
private final LogBuilder log;
Expand Down Expand Up @@ -114,7 +111,6 @@ protected CodeContext(CodeConnector<D> connector, boolean requireClock) {
ports = connector.extractPorts();
refs = connector.extractRefs();
info = connector.extractInfo();
componentType = connector.extractComponentType();
delegate = connector.getDelegate();
log = new LogBuilder(LogLevel.ERROR);
this.requireClock = requireClock || connector.requiresClock();
Expand Down Expand Up @@ -404,15 +400,6 @@ protected ComponentInfo getInfo() {
return info;
}

/**
* Get the component type.
*
* @return component type
*/
protected final ComponentType getComponentType() {
return componentType;
}

/**
* Find the address of the passed in control, or null if it does not have
* one.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2021 Neil C Smith.
* Copyright 2024 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 only, as
Expand Down Expand Up @@ -37,9 +37,16 @@
* Should make use of a {@link CodeCompilerService} implementation for compiling
* source code (which does support other processes).
*/
public class CodeContextFactoryService implements Service {
public final class CodeContextFactoryService implements Service {

/**
* Control ID of the new context control.
*/
public final static String NEW_CONTEXT = "new-context";

/**
* ControlInfo for the new context control.
*/
public final static ControlInfo NEW_CONTEXT_INFO
= ControlInfo.createFunctionInfo(
List.of(PReference.info(Task.class)),
Expand Down Expand Up @@ -87,7 +94,7 @@ public Task(CodeFactory<D> factory,
Class<D> previous) {
this(factory, code, logLevel, previous, null);
}

/**
* Create task.
*
Expand Down Expand Up @@ -147,13 +154,12 @@ public Class<D> getPrevious() {

/**
* Get the shared code classloader to use as parent (optional).
*
*
* @return shared classloader, or null
*/
public ClassLoader getSharedClassLoader() {
return sharedClassLoader;
}


}

Expand All @@ -180,7 +186,7 @@ public Result(CodeContext<D> context, LogBuilder log) {

/**
* Get created context.
*
*
* @return context
*/
public CodeContext<D> getContext() {
Expand All @@ -189,7 +195,7 @@ public CodeContext<D> getContext() {

/**
* Get log builder with any warning or error messages.
*
*
* @return log
*/
public LogBuilder getLog() {
Expand Down
Loading

0 comments on commit fcaf359

Please sign in to comment.