Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fvilla committed Dec 24, 2024
1 parent a9019a4 commit e2ad3bc
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.integratedmodelling.klab.api.geometry.Geometry;
import org.integratedmodelling.klab.api.knowledge.Artifact;
import org.integratedmodelling.klab.api.knowledge.KlabAsset.KnowledgeClass;
import org.integratedmodelling.klab.api.services.runtime.Notification;
import org.integratedmodelling.klab.api.services.runtime.extension.KlabFunction;

/**
Expand Down Expand Up @@ -115,6 +116,7 @@ interface Argument extends Serializable {
*/
List<Artifact.Type> getType();


/**
* Final arguments are set only once and mandatorily.
*
Expand Down Expand Up @@ -176,6 +178,12 @@ interface Argument extends Serializable {
*/
Collection<KnowledgeClass> getTargets();

/**
* Supported media types when appropriate (usually within import/export schemata)
* @return
*/
Set<String> getMediaTypes();

/**
* Service description if provided; empty string otherwise.
*
Expand Down Expand Up @@ -250,7 +258,7 @@ interface Argument extends Serializable {
* @return the collection of errors, each being a pair of <message, level>, or an empty collection if no
* errors occurred
*/
List<Pair<String, Level>> validate(ServiceCall function);
List<Notification> validate(ServiceCall function);

/**
* Full synopsis - expecting a multi-line string with full description of options, arguments and
Expand All @@ -259,29 +267,15 @@ interface Argument extends Serializable {
*
* @return longer description
*/
public String synopsis(Integer... flags);
String synopsis(Integer... flags);

/**
* One-line short synopsis intended to document usage without descriptions.
*
* @return short synopsis
*/
public String shortSynopsis();

// /**
// * Get the class of the object whose API the prototype specifies.
// *
// * @return Java class of object returned
// */
// public Class<?> executorClass();
//
// /**
// * If this is not null, the arguments are for the method and not for class construction.
// *
// * @return
// */
// public String getExecutorMethod();
//
String shortSynopsis();

/**
* If distributed, the service identified can be broadcast to multiple endpoints and the results can be
* merged.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,7 @@ ExternalAuthenticationCredentials.CredentialInfo addCredentials(String host,
* @param options
* @return
*/
InputStream exportAsset(String urn, KlabAsset.KnowledgeClass assetClass, String mediaType, Scope scope,
Object... options);
InputStream exportAsset(String urn, ResourceTransport.Schema exportSchema, String mediaType, Scope scope);

/**
* @param schema a valid schema that comes from those admitted in the service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.integratedmodelling.klab.api.knowledge.KlabAsset;
import org.integratedmodelling.klab.api.lang.ServiceInfo;
import org.integratedmodelling.klab.api.scope.Scope;
import org.integratedmodelling.klab.api.services.KlabService;
import org.integratedmodelling.klab.api.utils.Utils;

import java.io.File;
Expand Down Expand Up @@ -143,6 +144,7 @@ public static Schema create(String schemaId, Type type,
Schema ret = new Schema();
ret.setType(type);
ret.setSchemaId(schemaId);
ret.setKnowledgeClass(knowledgeClassDefined);
ret.setDescription(description);
return ret;
}
Expand Down Expand Up @@ -304,22 +306,31 @@ public List<Schema> findImportSchemata(String schemaId, String mediaType,
}

/**
* Find the export schemata within those identified with <code>schemaId</code> that are compatible with
* the passed media type and identity. If mediaType isn't null, the result should be unique.
* Find the export schemata applicable to the passed media type and identity. TODO use scope for
* permissions
*
* @param schemaId
* @param knowledgeClass
* @param mediaType
* @param identity
* @return
*/
public List<Schema> findExportSchemata(String schemaId, String mediaType,
AuthenticatedIdentity identity) {
return findSchemata(exportSchemata, schemaId, mediaType, identity);
public List<Schema> findExportSchemata(KlabAsset.KnowledgeClass knowledgeClass, String mediaType,
KlabService.ServiceCapabilities serviceCapabilities, Scope scope) {

var ret = new ArrayList<Schema>();
for (var schemaId : serviceCapabilities.getExportSchemata().keySet()) {
for (var schema : serviceCapabilities.getExportSchemata().get(schemaId)) {
if (schema.mediaTypes.contains(mediaType) && (knowledgeClass == null || schema.knowledgeClass == knowledgeClass)) {
ret.add(schema);
}
}
}
return ret;
}

// TODO pass scope for permissions
private List<Schema> findSchemata(Map<String, List<Schema>> schemata, String schemaId, String mediaType
, AuthenticatedIdentity identity) {

List<Schema> ret = new ArrayList<>();
if (schemata.containsKey(schemaId)) {
for (var schema : schemata.get(schemaId)) {
Expand All @@ -344,6 +355,7 @@ public void registerImportSchema(ServiceInfo serviceInfo) {
var type = serviceInfo.listArguments().isEmpty() ? Schema.Type.STREAM : Schema.Type.PROPERTIES;
var schema = Schema.create(serviceInfo.getName(), type, serviceInfo.getTargets().iterator().next(),
serviceInfo.getDescription());
schema.getMediaTypes().addAll(serviceInfo.getMediaTypes());

for (var arg : serviceInfo.listArguments()) {
schema = schema.with(arg.getName(), arg.getType().getFirst(), arg.isOptional());
Expand All @@ -357,7 +369,7 @@ public void registerExportSchema(ServiceInfo serviceInfo) {
var type = serviceInfo.listArguments().isEmpty() ? Schema.Type.STREAM : Schema.Type.PROPERTIES;
var schema = Schema.create(serviceInfo.getName(), type, serviceInfo.getTargets().iterator().next(),
serviceInfo.getDescription());

schema.getMediaTypes().addAll(serviceInfo.getMediaTypes());
// TODO revise, this is copied from import
for (var arg : serviceInfo.listArguments()) {
schema = schema.with(arg.getName(), arg.getType().getFirst(), arg.isOptional());
Expand All @@ -366,7 +378,6 @@ public void registerExportSchema(ServiceInfo serviceInfo) {
addExport(namespace, schema);
}


/**
* Adds the known formats. Others may be added by processing adapter import/export annotations or
* components.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.integratedmodelling.klab.api.knowledge.KlabAsset.KnowledgeClass;
import org.integratedmodelling.klab.api.lang.ServiceCall;
import org.integratedmodelling.klab.api.lang.ServiceInfo;
import org.integratedmodelling.klab.api.services.runtime.Notification;
import org.integratedmodelling.klab.api.services.runtime.extension.KlabFunction;
import org.integratedmodelling.klab.api.utils.Utils;

Expand Down Expand Up @@ -189,7 +190,6 @@ public void setUnit(String unit) {
// stable ordering reflecting that of the declaration
private Map<String, ArgumentImpl> arguments = new LinkedHashMap<>();
private String description;
// private Class<?> implementation;
private List<Type> type = new ArrayList<>();
private Geometry geometry;
private boolean distributed;
Expand All @@ -202,9 +202,9 @@ public void setUnit(String unit) {
private List<ArgumentImpl> outputAnnotations = new ArrayList<>();
private boolean isConst;
private boolean reentrant;
// private String executorMethod;
private FunctionType functionType;
private Set<KnowledgeClass> targets = EnumSet.noneOf(KnowledgeClass.class);
private Set<String> mediaTypes = new HashSet<>();

public String getLabel() {
return label;
Expand Down Expand Up @@ -263,13 +263,13 @@ public List<ServiceInfo.Argument> listArguments() {
}

@Override
public List<Pair<String, Level>> validate(ServiceCall function) {
List<Pair<String, Level>> ret = new ArrayList<>();
public List<Notification> validate(ServiceCall function) {
List<Notification> ret = new ArrayList<>();
// validate existing arguments
for (String arg : function.getParameters().keySet()) {
ArgumentImpl argument = arguments.get(arg);
if (argument == null) {
ret.add(Pair.of(name + ": argument " + arg + " is not recognized", Level.SEVERE));
ret.add(Notification.error(name + ": argument " + arg + " is not recognized"));
} else {
Object val = function.getParameters().get(arg);
// if ((val = classify(val, argument)) == null) {
Expand Down Expand Up @@ -637,6 +637,19 @@ public void setFunctionType(FunctionType functionType) {
this.functionType = functionType;
}

public void setTargets(Set<KnowledgeClass> targets) {
this.targets = targets;
}

@Override
public Set<String> getMediaTypes() {
return mediaTypes;
}

public void setMediaTypes(Set<String> mediaTypes) {
this.mediaTypes = mediaTypes;
}

@Override
public Collection<KnowledgeClass> getTargets() {
return this.targets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,11 @@ protected Set<Message.Queue> getQueuesFromHeader(SessionScope scope, String resp
}

@Override
public InputStream exportAsset(String urn, KlabAsset.KnowledgeClass knowledgeClass, String mediaType, Scope scope, Object... options) {
public InputStream exportAsset(String urn, ResourceTransport.Schema exportSchema, String mediaType, Scope scope) {
try {
var file =
client.withScope(scope).accepting(List.of(mediaType)).download(ServicesAPI.EXPORT,
"urn", urn, "class", knowledgeClass.name());
"urn", urn, "class", exportSchema.getKnowledgeClass().name());
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public Map<Setting, Object> getSettings() {
}

@Override
public InputStream exportAsset(String urn, KlabAsset.KnowledgeClass knowledgeClass, String mediaType, Scope scope, Object... options) {
public InputStream exportAsset(String urn, ResourceTransport.Schema exportSchema, String mediaType, Scope scope) {
// TODO establish which service we're targeting and route the request to it
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.integratedmodelling.klab.api.collections.Pair;
import org.integratedmodelling.klab.api.data.Version;
import org.integratedmodelling.klab.api.engine.StartupOptions;
import org.integratedmodelling.klab.api.exceptions.KlabAuthorizationException;
import org.integratedmodelling.klab.api.exceptions.KlabIllegalArgumentException;
import org.integratedmodelling.klab.api.exceptions.KlabIllegalStateException;
import org.integratedmodelling.klab.api.exceptions.KlabUnimplementedException;
Expand Down Expand Up @@ -661,8 +662,21 @@ public boolean loadComponents(ResourceSet resourceSet, Scope scope) {
return false;
}

final String mediaType = "application/java-archive";
var schemata =
ResourceTransport.INSTANCE.findExportSchemata(KlabAsset.KnowledgeClass.COMPONENT,
mediaType, service.capabilities(scope), scope);
if (schemata.isEmpty()) {
throw new KlabAuthorizationException("No authorized export schema with media type " + mediaType +
" is available");
} else if (schemata.size() > 1) {
scope.warn("Ambiguous request: more than one export schema with " +
"media type " + mediaType + " is available");
}


File plugin = new File(pluginPath + File.separator + result.getResourceUrn() + ".jar");
try (var input = service.exportAsset(result.getResourceUrn(), KlabAsset.KnowledgeClass.COMPONENT, "application/java-archive",
try (var input = service.exportAsset(result.getResourceUrn(), schemata.getFirst(), mediaType,
scope);
var output = new FileOutputStream(plugin)) {
IOUtils.copy(input, output);
Expand Down Expand Up @@ -790,6 +804,9 @@ private ServiceInfo createAnnotationPrototype(String namespacePrefix, Exporter a
ret.setDescription(annotation.description());
ret.setFunctionType(ServiceInfo.FunctionType.FREEFORM);
ret.getTargets().add(annotation.knowledgeClass());
if (annotation.mediaType() != null) {
ret.getMediaTypes().add(annotation.mediaType());
}

for (KlabFunction.Argument argument : annotation.properties()) {
var arg = createArgument(argument);
Expand All @@ -811,7 +828,9 @@ private ServiceInfo createAnnotationPrototype(String namespacePrefix, Importer a
ret.setDescription(annotation.description());
ret.setFunctionType(ServiceInfo.FunctionType.FREEFORM);
ret.getTargets().add(annotation.knowledgeClass());

if (annotation.mediaType() != null) {
ret.getMediaTypes().add(annotation.mediaType());
}
for (KlabFunction.Argument argument : annotation.properties()) {
var arg = createArgument(argument);
ret.getArguments().put(arg.getName(), arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public static String importComponentMaven(Parameters<String> properties, BaseSer
return null;
}

@Exporter(schema = "jar", description = "Export a component as a jar archive", mediaType = "application" +
"/java-archive", knowledgeClass =
@Exporter(schema = "jar", description = "Export a component as a jar archive", mediaType = "application/java-archive", knowledgeClass =
KlabAsset.KnowledgeClass.COMPONENT)
public static InputStream exportComponentDirect(String componentId, BaseService service, Scope scope) {
var componentRegistry = service.getComponentRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class GridImpl implements Grid {

private List<Pair<Double, Double>> anchorPoints = new ArrayList<>();
private final List<Pair<Double, Double>> anchorPoints = new ArrayList<>();

public static GridImpl promote(Grid grid) {
if (grid instanceof GridImpl grid1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import org.integratedmodelling.klab.api.lang.ServiceCall;
import org.locationtech.jts.geom.Geometry;

import java.io.Serial;
import java.util.Collection;


public class MeshImpl extends ShapeImpl implements Mesh {

@Serial
private static final long serialVersionUID = -7906419063910020731L;
private Collection<Shape> features;
private final Collection<Shape> features;

public MeshImpl(Geometry geometry, Projection projection, Collection<Shape> features) {
super(geometry, projection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ProjectionImpl implements Projection {

private static Projection defaultProjection;
private static Projection latlonProjection;
private static Projection[][] utmProjections = new Projection[2][60];
private static final Projection[][] utmProjections = new Projection[2][60];

public ProjectionImpl(String definition) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
import org.locationtech.jts.simplify.TopologyPreservingSimplifier;

import java.io.IOException;
import java.io.Serial;
import java.io.StringWriter;
import java.util.*;

public class ShapeImpl extends SpaceImpl implements Shape {

@Serial
private static final long serialVersionUID = 5154895981013940462L;

protected Geometry geometry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
import org.integratedmodelling.klab.runtime.scale.ExtentImpl;
import org.locationtech.jts.geom.GeometryFactory;

import java.io.Serial;
import java.util.List;
import java.util.Map;

public abstract class SpaceImpl extends ExtentImpl<Space> implements Space {

@Serial
private static final long serialVersionUID = 1L;

static GeometryFactory gFactory = new GeometryFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKBWriter;

import java.io.Serial;
import java.util.Arrays;

public class TileImpl extends ShapeImpl implements Tile {

@Serial
private static final long serialVersionUID = -645107030417341241L;
private Grid grid;
private long size = 1;
Expand Down
Loading

0 comments on commit e2ad3bc

Please sign in to comment.